package com.trsj.lnsj.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public WebMvcConfig() {
        super();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截规则：除了api路径，其他都拦截判断
        registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**").excludePathPatterns("/api");
        // registry.addInterceptor(new
        // JWTInterceptor()).excludePathPatterns("/api/user/login");

        super.addInterceptors(registry);
    }

    /*
     * @Bean public InternalResourceViewResolver viewResolver(){
     * InternalResourceViewResolver viewResolver = new
     * InternalResourceViewResolver();
     * viewResolver.setPrefix("/WEB-INF/classes/views/");
     * viewResolver.setSuffix(".html"); viewResolver.setViewClass(JstlView.class);
     * return viewResolver; }
     */

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/js/");
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/");
        super.addResourceHandlers(registry);
    }

}
